<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>去掉数组中的某一部分并用其它值取代</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.array-slice.html">« array_slice</a></li>
      <li style="float: right;"><a href="function.array-sum.html">array_sum »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.array.html">数组 函数</a></li>
    <li>去掉数组中的某一部分并用其它值取代</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.array-splice" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">array_splice</h1>
    <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">array_splice</span> &mdash; <span class="dc-title">
     去掉数组中的某一部分并用其它值取代
    </span></p>

   </div>
 <div class="refsect1 description" id="refsect1-function.array-splice-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>array_splice</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">array</span> <code class="parameter reference">&$array</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$offset</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">?</span><span class="type"><span class="type">int</span><span class="type"></span></span> <code class="parameter">$length</code><span class="initializer"> = <strong><code>null</code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$replacement</code><span class="initializer"> = []</span></span><br>): <span class="type">array</span></div>

    <p class="para rdfs-comment">
     把 <code class="parameter">array</code> 数组中由
     <code class="parameter">offset</code> 和 <code class="parameter">length</code>
     指定的单元去掉，如果提供了 <code class="parameter">replacement</code> 参数，则用其中的单元取代。
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
    <p class="para">
       <code class="parameter">array</code> 中的数字键名不被保留。
    </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    如果 <code class="parameter">replacement</code> 不是数组，会被
    <a href="language.types.array.html#language.types.array.casting" class="link">类型转换</a>
   成数组 (例如： <code class="code">(array) $replacement</code>)。
    当传入的 <code class="parameter">replacement</code> 是个对象或者 <strong><code>null</code></strong>，会导致未知的行为出现。
    </span>
  </p></blockquote>
  </div>

  <div class="refsect1 parameters" id="refsect1-function.array-splice-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">array</code></dt>

     <dd>

      <p class="para">
       输入的数组。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">offset</code></dt>

     <dd>

      <p class="para">
       如果 <code class="parameter">offset</code> 为正，则从 <code class="parameter">array</code>
     数组中该值指定的偏移量开始移除。
      </p>
      <p class="para">
       如果 <code class="parameter">offset</code>
     为负，则从 <code class="parameter">array</code> 末尾倒数该值指定的偏移量开始移除。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">length</code></dt>

     <dd>

      <p class="para">
       如果省略 <code class="parameter">length</code>，则移除数组中从 <code class="parameter">offset</code>
     到结尾的所有部分。
      </p>
      <p class="para">
       如果指定了 <code class="parameter">length</code>
     并且为正值，则移除这么多单元。
      </p>
      <p class="para">
       如果指定了 <code class="parameter">length</code>
     并且为负值，则移除部分停止于数组末尾该数量的单元。
      </p>
      <p class="para">
      如果设置了 <code class="parameter">length</code> 为零，不会移除单元。
      </p>
      <div class="tip"><strong class="tip">小技巧</strong>
       <p class="para">
        当给出了
      <code class="parameter">replacement</code> 时要移除从 <code class="parameter">offset</code>
      到数组末尾所有单元时，用 <code class="literal">count($input)</code> 作为 <code class="parameter">length</code>。
       </p>
      </div>
     </dd>

    
    
     <dt>
<code class="parameter">replacement</code></dt>

     <dd>

      <p class="para">
       如果给出了 <code class="parameter">replacement</code> 数组，则被移除的单元被此数组中的单元替代。
      </p>
      <p class="para">
       如果
        <code class="parameter">offset</code> 和 <code class="parameter">length</code>
        的组合结果是不会移除任何值，则 <code class="parameter">replacement</code>
        数组中的单元将被插入到 <code class="parameter">offset</code>
        指定的位置。
      </p>
      <blockquote class="note"><p><strong class="note">注意</strong>: 
       <p class="para">
        不保留替换数组 <code class="parameter">replacement</code> 中的键名。
        </p>
      </p></blockquote>
      <p class="para">
       如果用来替换 <code class="parameter">replacement</code> 只有一个单元，那么不需要给它加上
        <code class="literal">array()</code> 或方括号，除非该单元本身就是一个数组、一个对象或者 <strong><code>null</code></strong>。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.array-splice-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
    返回一个包含有被移除单元的数组。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.array-splice-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       <code class="parameter">length</code> 现在可为空（nullable）。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.array-splice-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4956">
    <p><strong>示例 #1 <span class="function"><strong>array_splice()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$input&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"red"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"green"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"blue"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"yellow"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_splice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$input&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"red"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"green"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"blue"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"yellow"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_splice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$input&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"red"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"green"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"blue"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"yellow"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_splice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"orange"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$input&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"red"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"green"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"blue"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"yellow"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_splice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #DD0000">"black"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"maroon"</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(2) {
  [0]=&gt;
  string(3) &quot;red&quot;
  [1]=&gt;
  string(5) &quot;green&quot;
}
array(2) {
  [0]=&gt;
  string(3) &quot;red&quot;
  [1]=&gt;
  string(6) &quot;yellow&quot;
}
array(2) {
  [0]=&gt;
  string(3) &quot;red&quot;
  [1]=&gt;
  string(6) &quot;orange&quot;
}
array(5) {
  [0]=&gt;
  string(3) &quot;red&quot;
  [1]=&gt;
  string(5) &quot;green&quot;
  [2]=&gt;
  string(4) &quot;blue&quot;
  [3]=&gt;
  string(5) &quot;black&quot;
  [4]=&gt;
  string(6) &quot;maroon&quot;
}
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4957">
    <p><strong>示例 #2 几个以不同表达式实现相同效果的 <span class="function"><strong>array_splice()</strong></span> 例子</strong></p>
    <div class="example-contents"><p>
     以下表达式是相同的：
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">//&nbsp;添加两个新元素到&nbsp;$input<br /></span><span style="color: #0000BB">array_push</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$x</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$y</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_splice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #0000BB">$x</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$y</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">//&nbsp;移除&nbsp;$input&nbsp;中的最后一个元素<br /></span><span style="color: #0000BB">array_pop</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_splice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;移除&nbsp;$input&nbsp;中第一个元素<br /></span><span style="color: #0000BB">array_shift</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_splice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;在&nbsp;$input&nbsp;的开头插入一个元素<br /></span><span style="color: #0000BB">array_unshift</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$x</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$y</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_splice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #0000BB">$x</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$y</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">//&nbsp;在&nbsp;$input&nbsp;的索引&nbsp;$x&nbsp;处替换值<br /></span><span style="color: #0000BB">$input</span><span style="color: #007700">[</span><span style="color: #0000BB">$x</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">$y</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;对于键名和偏移量等值的数组<br /></span><span style="color: #0000BB">array_splice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$x</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$y</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.array-splice-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.array-merge.html" class="function" rel="rdfs-seeAlso">array_merge()</a> - 合并一个或多个数组</span></li>
    <li class="member"><span class="function"><a href="function.array-slice.html" class="function" rel="rdfs-seeAlso">array_slice()</a> - 从数组中取出一段</span></li>
    <li class="member"><span class="function"><a href="function.unset.html" class="function" rel="rdfs-seeAlso">unset()</a> - 清除指定变量</span></li>
   </ul>
  </p>
 </div>
  
</div></div></div></body></html>